package com.example.easytts.service

import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Service
import org.springframework.web.reactive.function.client.WebClient
import org.springframework.http.MediaType
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue

@Service
class AzureTtsService(
    @Value("\${azure.speech.key}")
    private val subscriptionKey: String,
    @Value("\${azure.speech.region}")
    private val region: String,
    @Value("\${azure.speech.guest.key:\${azure.speech.key}}")
    private val guestSubscriptionKey: String,
    @Value("\${azure.speech.guest.region:\${azure.speech.region}}")
    private val guestRegion: String
) {
    private val webClient = WebClient.builder()
        .baseUrl("http://172.17.0.4:5001")
        //.baseUrl("http://127.0.0.1:5001")
        .defaultHeader("User-Agent", "easytts")
        .build()

    private val objectMapper = jacksonObjectMapper()

    fun synthesizeSpeech(text: String, voice: String, language: String, useSsml: Boolean, isMember: Boolean = false): String {
        println("$text $voice $language isMember: $isMember")
        
        // 根据会员状态选择不同的配置
        val key = if (isMember) subscriptionKey else guestSubscriptionKey
        val serviceRegion = if (isMember) region else guestRegion
        
        val requestBody = mapOf(
            "text" to text,
            "voice" to voice,
            "speech_key" to key,
            "service_region" to serviceRegion
        )
        val response = webClient.post()
            .uri("/api/tts/azure")
            .contentType(MediaType.APPLICATION_JSON)
            .bodyValue(requestBody)
            .retrieve()
            .bodyToMono(String::class.java)
            .block() ?: throw RuntimeException("Failed to synthesize speech via local Flask service")
        
        val responseMap = objectMapper.readValue<Map<String, Any>>(response)
        return responseMap["cos_url"] as? String ?: throw RuntimeException("Invalid response format: missing cos_url")
    }

    fun synthesizeSpeechWithLrc(
        text: String,
        voice: String,
        language: String,
        useSsml: Boolean,
        isMember: Boolean = false
    ): Map<String, Any?> {
        val key = if (isMember) subscriptionKey else guestSubscriptionKey
        val serviceRegion = if (isMember) region else guestRegion

        val requestBody = mapOf(
            "text" to text,
            "voice" to voice,
            "speech_key" to key,
            "service_region" to serviceRegion
        )
        val response = webClient.post()
            .uri("/api/tts/azure/lrc")
            .contentType(MediaType.APPLICATION_JSON)
            .bodyValue(requestBody)
            .retrieve()
            .bodyToMono(String::class.java)
            .block() ?: throw RuntimeException("Failed to synthesize speech with lrc")

        val responseMap = objectMapper.readValue<Map<String, Any?>>(response)
        return responseMap
    }
} 